package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdateVisitorServlet
 */
@WebServlet("/UpdateVisitorServlet")
public class UpdateVisitorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorUpdate.jsp");  
		rd.forward(request,response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");   
	    
	    HttpSession session=request.getSession(true);
	    
	    
	    Visitor u =new Visitor();
	    
	    
	    u.setVisitorName((String)session.getAttribute("visitorName"));
	    u.setPassword((String)session.getAttribute("password"));
	    u.setIdVisitor((Integer) session.getAttribute("idVisitor"));
		u.setFirstName((String)session.getAttribute("firstName"));
		u.setLastName((String)session.getAttribute("lastName"));
		u.setEmail((String)session.getAttribute("email"));
		u.setPhone((Long)session.getAttribute("phone"));
		u.setAddress((String)session.getAttribute("address"));
		u.setDni((String)session.getAttribute("dni"));
	    
	    
	    
	    
	    if(request.getParameter("firstname").equals("")==false)
	    {
	    	
	    u.setFirstName(request.getParameter("firstname"));
	    }
	    if(request.getParameter("lastname").equals("")==false)
	    {
	    u.setLastName(request.getParameter("lastname"));
	    }
	    if(request.getParameter("emailAddress").equals("")==false)
	    {
	    	u.setEmail(request.getParameter("emailAddress"));
	    }
	    if(request.getParameter("phone").equals("")==false)
	    {
	    
	    	u.setPhone(	Long.parseLong(request.getParameter("phone"))	);
	    }
	    if(request.getParameter("dni").equals("")==false)
	    {
	    	u.setDni(request.getParameter("dni"));
	    }
	    if(request.getParameter("addres").equals("")==false)
	    {
	    	u.setAddress(request.getParameter("addres"));
	    }
	    if(request.getParameter("username").equals("")==false)
	    {
	    	
	    u.setVisitorName(request.getParameter("username"));
	    
	    }
	    VisitorService vs = new VisitorService();
	    
	    try
	    {
	    	
	    	if(session.getAttribute("password").equals(request.getParameter("password"))){
	    		
	    		
	    		
	    		if(vs.updateVisitor(u))
	    		{
	    			session.setAttribute("visitorName",u.getVisitorName());
		    		session.setAttribute("password",u.getPassword());
		    		session.setAttribute("idVisitor", u.getIdVisitor());
		    		session.setAttribute("firstName",u.getFirstName());
		    		session.setAttribute("lastName",u.getLastName());
		    		session.setAttribute("email",u.getEmail());
		    		session.setAttribute("phone", u.getPhone());
		    		session.setAttribute("address", u.getAddress());
		    		session.setAttribute("dni", u.getDni());
	    			RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    			rd.forward(request,response);	
	    		}
	    		else{
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorUpdate.jsp");  
    			rd.forward(request,response);
	    		}
	    	}
	    	else
	    	{
	    		session.setAttribute("error", "Incorrect password");
	    		 
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_showEvents.jsp");  
	    		rd.include(request,response);

	    	}
 	
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }

	}

}
